#include "LLogicProperty.h"
#include "common/PLog.h"
#include "scene/SceneBase.h"
#include "scene/LayerCUnEvent.h"

CLLogicProperty::CLLogicProperty(void)
{
}


CLLogicProperty::~CLLogicProperty(void)
{
}

void CLLogicProperty::LogicInit() {
	AddLogicType(GLSLTPersonProperty);
	AddLogicType(GLSLTBodyProperty);
}

bool CLLogicProperty::CatchLogic(const ProtocolBase *head, char * data, uint32_t len, void * connect) {
	if (!this->ExistLogicType(head->m_Logic))
		return false;
	//LSocket* tConnect = (LSocket*)(connect);
	if (head->m_From == GLPFromServer) {
		switch (head->m_Logic) {
		case GLSLTPersonProperty:
			switch (head->m_Action) {
			case GLPAChange:
				onChangeUserProperty(head, data, len);
				break;
			default:
				break;
			}
			break;
		case GLSLTBodyProperty:
			switch (head->m_Action) {
			case GLPAChange:
				onChangeBodyProperty(head, data, len);
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	} else {

	}
	return true;
}

void CLLogicProperty::AddPoint(FullPointInfo& fullPoint, int32_t store, uint8_t bPointCount, PropertyInfo* bPoint, uint8_t oPointCount, PropertyInfo* oPoint) {
	CCLOG("add|store|point|%d", store);
	fullPoint.m_StorePoint += store;
	for (uint8_t i = 0; i != bPointCount; ++i) {
		fullPoint.m_BasePoint.AllPM[bPoint->m_Type] += bPoint->m_Value;
		++bPoint;
		CCLOG("add|base|point|type|%d|value|%d", bPoint->m_Type, bPoint->m_Value);
	}
	for (uint8_t i = 0; i != oPointCount; ++i) {
		fullPoint.m_OtherPoint.AllPM[oPoint->m_Type] += oPoint->m_Value;
		++oPoint;
		CCLOG("add|other|point|type|%d|value|%d", oPoint->m_Type, oPoint->m_Value);
	}
}

void CLLogicProperty::AddProperty(BaseProperty& property, uint8_t count, PropertyInfo* prop) {
	PropertyInfo* tProperty = prop;
	for (uint8_t i = 0; i != count; ++i) {
		property.AllProp[tProperty->m_Type] += tProperty->m_Value;
		CCLOG("add|property|type|%d|value|%d", tProperty->m_Type, tProperty->m_Value);
		++tProperty;
	}

	CLayerCUnEvent* tLayer = (CLayerCUnEvent*)m_LayerManager->getCommonLayer(CLayerManager::CommonUnEventLayer);
	if (nullptr == tLayer) {
		CCLOG("layer|null|%d|%s", CLayerManager::CommonUnEventLayer, __FUNCTION__);
		return;
	}
	tLayer->BroadcastProperty(count, prop);
}

void CLLogicProperty::onChangeUserProperty(const ProtocolBase *head, char * data, uint32_t len) {
	CCLOG("change|property|uid|%ld", head->m_UserId);
	ChangePropertyResp* tChange = (ChangePropertyResp*)(data);
	AddPoint(m_Oneself->GetFullPoint(), tChange->m_StorePoint, tChange->m_BasePointCount, tChange->m_ChangeBasePoint, tChange->m_OtherPointCount, tChange->m_ChangeOtherPoint);
	AddProperty(m_Oneself->GetProperty(), tChange->m_PropCount, tChange->m_ChangeProp);
}

void CLLogicProperty::onChangeBodyProperty(const ProtocolBase* head, char * data, uint32_t len) {
	CCLOG("change|property|bid|%ld", head->m_UserId);
	ChangePropertyResp* tChange = (ChangePropertyResp*)(data);
	CGLBody* tBody = m_LogicData->GetMyBody(head->m_UserId);
	if (nullptr == tBody) {
		CCLOG("body|none|uid|%ld", head->m_UserId);
		return;
	}
	AddPoint(tBody->GetFullPoint(), tChange->m_StorePoint, tChange->m_BasePointCount, tChange->m_ChangeBasePoint, tChange->m_OtherPointCount, tChange->m_ChangeOtherPoint);
	AddProperty(tBody->GetProperty(), tChange->m_PropCount, tChange->m_ChangeProp);
}
